 package com.project.myxima.presenters;

import android.widget.Toast;

import com.project.myxima.data.XimalayaApi;
import com.project.myxima.base.BaseApplication;
import com.project.myxima.interfaces.ISearchCallback;
import com.project.myxima.interfaces.ISearchPresenter;
import com.project.myxima.utils.LogUtils;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.SearchAlbumList;
import com.ximalaya.ting.android.opensdk.model.word.HotWord;
import com.ximalaya.ting.android.opensdk.model.word.HotWordList;
import com.ximalaya.ting.android.opensdk.model.word.QueryResult;
import com.ximalaya.ting.android.opensdk.model.word.SuggestWords;

import java.util.ArrayList;
import java.util.List;

public class SearchPresenter implements ISearchPresenter {

    private List<ISearchCallback> mCallbacks = new ArrayList<>();

    private final XimalayaApi mXimalayaApi;
    private String mKeyword;
    private int mCurrentPage = 1;
    private String TAG = "SearchPresenter";
    private boolean isLoadMore = false;
    private int DEFAULT_PAGE = 1;

    //single instance for SearchPresenter
    private SearchPresenter(){
        mXimalayaApi = XimalayaApi.getXimalayaApi();
    }

    private static class SearchPresenterHolder{
        public static final SearchPresenter SEARCH_PRESENTER = new SearchPresenter();
    }

    public static SearchPresenter getSearchPresenter(){
        return SearchPresenterHolder.SEARCH_PRESENTER;
    }


    @Override
    public void doSearch(String keyword) {
        this.mKeyword = keyword;
        searchByKeyword(keyword,false);
    }

    private void searchByKeyword(String keyword, final boolean isLoadMore){

        if(!isLoadMore){
            mCurrentPage = DEFAULT_PAGE;
        }else{
            mCurrentPage++;
        }

        mXimalayaApi.searchByKeyword(keyword, mCurrentPage, new IDataCallBack<SearchAlbumList>() {
            @Override
            public void onSuccess(SearchAlbumList searchAlbumList) {
                if (searchAlbumList != null) {
                    LogUtils.d(TAG,"---------->Successfully get " + searchAlbumList.getTotalCount() + "results");
                    for(ISearchCallback iSearchCallback:mCallbacks){
                        iSearchCallback.onSearchResultLoaded(searchAlbumList.getAlbums(),isLoadMore);
                    }
                }else{
                    LogUtils.d(TAG,"---------->results is null");
                }
            }

            @Override
            public void onError(int errorCode, String errorMsg) {
                for (ISearchCallback callback:mCallbacks){
                    callback.onError(errorCode,errorMsg);
                }

            }
        });
    }

    @Override
    public void reSearch() {
        searchByKeyword(mKeyword,false);
    }

    @Override
    public void loadMore() {
        //this is load more action, different from search
        searchByKeyword(mKeyword,true);
    }

    @Override
    public void getHotWord() {

        //todo:need a cache
        mXimalayaApi.getHotWord(new IDataCallBack<HotWordList>() {
            @Override
            public void onSuccess(HotWordList hotWordList) {
                //send hotwords to callback
                List<HotWord> hotWords = hotWordList.getHotWordList();
                for(ISearchCallback iSearchCallback : mCallbacks){
                    iSearchCallback.onHotWordLoaded(hotWords);
                }
            }

            @Override
            public void onError(int errorCode, String errorMsg) {
                //nothing to do
            }
        });
    }

    @Override
    public void getSuggestionWord(String word) {
        mXimalayaApi.getSuggestionWord(word, new IDataCallBack<SuggestWords>() {
            @Override
            public void onSuccess(SuggestWords suggestWords) {
                if (suggestWords != null) {
                    List<QueryResult> keyWordList = suggestWords.getKeyWordList();
                    //notify the UI layer
                    for(ISearchCallback iSearchCallback:mCallbacks){
                        iSearchCallback.onSuggestionWordLoaded(keyWordList);
                    }
                }

                LogUtils.d(TAG,"---------->Successfully get " + suggestWords.getAlbumTotalCount()+ "results");
            }

            @Override
            public void onError(int i, String s) {
                Toast.makeText(BaseApplication.getAppContext(),"Fail to Request SuggestionWord",Toast.LENGTH_LONG).show();
                LogUtils.d(TAG,"errorCode ---->" + i);
                LogUtils.d(TAG,"errorMsg ---->" + s);

            }
        });
    }

    @Override
    public void registerViewCallback(ISearchCallback iSearchCallback) {
        if(iSearchCallback != null && !mCallbacks.contains(iSearchCallback)){
            mCallbacks.add(iSearchCallback);
        }

    }

    @Override
    public void unregisterViewCallback(ISearchCallback iSearchCallback) {
        mCallbacks.remove(iSearchCallback);
    }
}
